/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.reviewerplugin;

import java.io.Serializable;
import java.util.List;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;

import com.pearreview.util.review.Aspect;
import com.pearreview.util.review.Finding;
import com.pearreview.util.review.IdList;
import com.pearreview.util.review.Product;
import com.pearreview.util.review.Severity;

public class Report implements Serializable {
	
	private static final long serialVersionUID = 5232705413560135756L;
	
	private EventListenerList changeListenerList;
	
	private IdList<Finding> findings;
	private IdList<Aspect> aspects;
	private List<Severity> severities;
	private Product product;
	private Long preparationTime;
	private String recommendation;
	private String impression;
	private String name;
	private String description;
	private String comments;
	
	public Report() {
		changeListenerList = new EventListenerList();
	}
	
	/**
	 * Adds a change listener
	 * @param cl listener to add
	 */
	public void addChangeListener(ChangeListener cl) {
		changeListenerList.add(ChangeListener.class, cl);
	}
	
	/**
	 * Removes change listener
	 * @param cl listener to remove
	 */
	public void removeChangeListener(ChangeListener cl) {
		changeListenerList.remove(ChangeListener.class, cl);
	}
	
	/**
	 * Return an array of all the change listeners. 
	 * @return
	 */
	protected ChangeListener[] getChangeListeners() {
		return changeListenerList.getListeners(ChangeListener.class);
	}
	
	protected void fireChangeListeners() {
		ChangeEvent e = new ChangeEvent(this);
		fireChangeListeners(e);
	}
	
	protected void fireChangeListeners(ChangeEvent e) {
		ChangeListener[] listeners = getChangeListeners();
		for (int i = 0; i < listeners.length ; i++ ) {
			listeners[i].stateChanged(e);
		}
	}
	
	private boolean equals(Object obj1, Object obj2) {
		if(obj1 == obj2) {
			return true;
		}
		
		return obj1 == null ? obj2 == null : obj1.equals(obj2);
	}

	public IdList<Finding> getFindings() {
		return findings;
	}
	public void setFindings(IdList<Finding> newFindings) {
		if(!equals(findings, newFindings)) {
			findings = newFindings;
			fireChangeListeners();
		}
	}
	
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product newProduct) {
		if(!equals(product, newProduct)) {
			product = newProduct;
			fireChangeListeners();
		}
	}
	
	public Long getPreparationTime() {
		return preparationTime;
	}
	public void setPreparationTime(Long newPreparationTime) {
		if(!equals(preparationTime, newPreparationTime)) {
			preparationTime = newPreparationTime;
			fireChangeListeners();
		}
	}
	
	public String getRecommendation() {
		return recommendation;
	}
	public void setRecommendation(String newRecommendation) {
		if(!equals(recommendation, newRecommendation)) {
			recommendation = newRecommendation;
			fireChangeListeners();
		}
	}
	
	public String getImpression() {
		return impression;
	}
	public void setImpression(String newImpression) {
		if(!equals(impression, newImpression)) {
			impression = newImpression;
			fireChangeListeners();
		}
	}
	
	public IdList<Aspect> getAspects() {
		return aspects;
	}
	public void setAspects(IdList<Aspect> newAspects) {
		if(!equals(aspects, newAspects)) {
			aspects = newAspects;
			fireChangeListeners();
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String newName) {
		if(!equals(name, newName)) {
			name = newName;
			fireChangeListeners();
		}
	}
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String newDescription) {
		if(!equals(description, newDescription)) {
			description = newDescription;
			fireChangeListeners();
		}
	}
	
	public String getComments() {
		return comments;
	}
	public void setComments(String newComments) {
		if(!equals(comments, newComments)) {
			comments = newComments;
			fireChangeListeners();
		}
	}
	
	public List<Severity> getSeverities() {
		return severities;
	}
	public void setSeverities(List<Severity> newSeverities) {
		if(!equals(severities, newSeverities)) {
			severities = newSeverities;
			fireChangeListeners();
		}
	}
}